//
//  RCTConvert+BaiduMapKit.m
//  YuanXinBaiduMap
//
//  Created by 晏德智 on 16/9/27.
//  Copyright © 2016年 晏德智. All rights reserved.
//

#import "RCTConvert+BaiduMapKit.h"
#if __has_include(<React/RCTBridgeModule.h>)
#import <React/RCTConvert+CoreLocation.h>
#else
#import "RCTConvert+CoreLocation.h"
#endif

#import "RCTConvert+BaiduMapKit.h"
#import "RCTBaiduMapAnnotation.h"
#import "RCTBaiduMapOverlay.h"

@implementation RCTConvert (BaiduMapKit)

RCT_ARRAY_CONVERTER(CLLocation)
+ (CLLocation *)CLLocation:(id)json
{
    json = [self NSDictionary:json];
    return [[CLLocation alloc] initWithLatitude:[self CLLocationDegrees:json[@"latitude"]] longitude:[self CLLocationDegrees:json[@"longitude"]]];
}

+ (BMKCoordinateSpan)BMKCoordinateSpan:(id)json
{
    json = [self NSDictionary:json];
    return (BMKCoordinateSpan){
        [self CLLocationDegrees:json[@"latitudeDelta"]],
        [self CLLocationDegrees:json[@"longitudeDelta"]]
    };
}

+ (BMKCoordinateRegion)BMKCoordinateRegion:(id)json
{
    return (BMKCoordinateRegion) {
        [self CLLocationCoordinate2D:json],
        [self BMKCoordinateSpan:json]
    };
}

RCT_ENUM_CONVERTER(BMKMapType, (@{
                                  @"standard": @(BMKMapTypeStandard),
                                  @"satellite": @(BMKMapTypeSatellite),
                                  }), BMKMapTypeStandard, integerValue)

+ (RCTBaiduMapAnnotation *)RCTBaiduMapAnnotation:(id)json
{
    json = [self NSDictionary:json];
    __block RCTBaiduMapAnnotation *annotation = [RCTBaiduMapAnnotation new];
    RCTExecuteOnMainQueue(^{
        annotation.coordinate = [self CLLocationCoordinate2D:json];
    });

    //annotation.coordinate = [self CLLocationCoordinate2D:json];
    annotation.draggable = [self BOOL:json[@"draggable"]];
    annotation.title = [self NSString:json[@"title"]];
    annotation.subtitle = [self NSString:json[@"subtitle"]];
    annotation.identifier = [self NSString:json[@"id"]];
    annotation.hasLeftCallout = [self BOOL:json[@"hasLeftCallout"]];
    annotation.hasRightCallout = [self BOOL:json[@"hasRightCallout"]];
    annotation.animateDrop = [self BOOL:json[@"animateDrop"]];
    annotation.tintColor = [self UIColor:json[@"tintColor"]];
    if(json[@"image"]){
        //annotation.image = [self UIImage:json[@"image"]];
    }
    annotation.viewIndex = [self NSInteger:json[@"viewIndex"] ? :@(NSNotFound)];
    annotation.leftCalloutViewIndex =
    [self NSInteger:json[@"leftCalloutViewIndex"] ?: @(NSNotFound)];
    annotation.rightCalloutViewIndex =
    [self NSInteger:json[@"rightCalloutViewIndex"] ?: @(NSNotFound)];
    annotation.detailCalloutViewIndex =
    [self NSInteger:json[@"detailCalloutViewIndex"] ?: @(NSNotFound)];
    return annotation;
}

RCT_ARRAY_CONVERTER(RCTBaiduMapAnnotation)

+ (RCTBaiduMapOverlay *)RCTBaiduMapOverlay:(id)json
{
    json = [self NSDictionary:json];
    
    NSArray<NSDictionary *> *locations = [self NSDictionaryArray:json[@"coordinates"]];
    CLLocationCoordinate2D coordinates[locations.count];
    NSUInteger index = 0;
    for (NSDictionary *location in locations) {
        coordinates[index++] = [self CLLocationCoordinate2D:location];
    }
    
    RCTBaiduMapOverlay *overlay = [RCTBaiduMapOverlay polylineWithCoordinates:coordinates count:locations.count];
    
    overlay.strokeColor = [self UIColor:json[@"strokeColor"]];
    overlay.identifier = [self NSString:json[@"id"]];
    overlay.lineWidth = [self CGFloat:json[@"lineWidth"] ?: @1];
    return overlay;
    
}

RCT_ARRAY_CONVERTER(RCTBaiduMapOverlay)

+ (BMKLocationViewDisplayParam *)RCTBaiduMapLocationViewDisplayParam:(id)json
{
    json = [self NSDictionary:json];
    BMKLocationViewDisplayParam *param = [BMKLocationViewDisplayParam new];
    param.locationViewOffsetX = [self float:json[@"offsetX"]];
    param.locationViewOffsetY = [self float:json[@"offsetY"]];
    param.isAccuracyCircleShow = [self BOOL:json[@"showAccuracyCircle"]];
    param.accuracyCircleFillColor = [self UIColor:json[@"accuracyCircleFillColor"]];
    param.accuracyCircleStrokeColor = [self UIColor:json[@"accuracyCircleStrokeColor"]];
    param.isRotateAngleValid = [self BOOL:json[@"rotateAngleValid"]];
    //    param.locationViewImgName = [self NSString:json[@"image"]];
    RCTImageSource *imageSource = [self RCTImageSource:json[@"image"]];
    if (imageSource != nil) {
        /*
         NSString *imgName = imageSource.imageURL.lastPathComponent;
         if (imgName != nil) {
         imgName = [imgName stringByDeletingPathExtension];
         param.locationViewImgName = imgName;
         }
         */
    }
    
    return param;
}


/*
 RCT_ARRAY_CONVERTER(BMKUserLocation)
 + (BMKUserLocation *)BMKUserLocation:(id)json{
 json = [self NSDictionary:json];
 BMKUserLocation *userLocation = [BMKUserLocation new];
 NSDictionary *locationData = [json NSDictionary:json[@"location"]];
 CLLocation *cclocation = nil;
 
 userLocation.location.;
 }
 */

+ (BaiduLocationOptions)BaiduLocationOptions:(id)json
{
    NSDictionary<NSString *, id> *options = [self NSDictionary:json];
    
    double distanceFilter = options[@"distanceFilter"] == NULL ? kCLLocationAccuracyHundredMeters
    : [RCTConvert double:options[@"distanceFilter"]] ?: kCLDistanceFilterNone;
    double headingFilter = options[@"headingFilter"] == NULL ? 1 :[RCTConvert double:options[@"headingFilter"]] ;
    return (BaiduLocationOptions){
        .timeout = [RCTConvert NSTimeInterval:options[@"timeout"]] ?: INFINITY,
        .maximumAge = [RCTConvert NSTimeInterval:options[@"maximumAge"]] ?: 0,//INFINITY,
        .accuracy = [RCTConvert BOOL:options[@"enableHighAccuracy"]] ? kCLLocationAccuracyBest : KK_DEFAULT_LOCATION_ACCURACY,
        .distanceFilter = distanceFilter,
        .headingFilter =headingFilter
    };
}


+ (BMKMapStatus *)BMKMapStatus:(id)json{
    json = [self NSDictionary:json];
    BMKMapStatus *mapStatus = [BMKMapStatus new];
    
    if(json[@"level"] != NULL){
        //缩放级别:[3~19]
        mapStatus.fLevel = [json float:@"maplevel"];
    }
    if(json[@"rotation"] != NULL){
        //旋转角度
        mapStatus.fRotation = [json float:@"maprotation"];
    }
    if(json[@"overlooking"] != NULL){
        //俯视角度:[-45~0]
        mapStatus.fOverlooking = [json float:@"mapoverlooking"];
    }
    if(json[@"screenCenter"] != NULL){
        //屏幕中心点坐标:在屏幕内，超过无效
        mapStatus.targetScreenPt = [json CGPoint:@"mapScreenCenter"];
    }
    if(json[@"mapCeater"] != NULL){
        //options[@"headingFilter"] == NULL
        // NSDictionary *targetGeoPt = [json NSDictionary:@"mapCeater"];
        mapStatus.targetGeoPt = [self CLLocationCoordinate2D:json[@"mapCeater"]];
    }
    
    return mapStatus;
}

RCT_ARRAY_CONVERTER(BMKMapStatus)

@end

